﻿using Microsoft.Win32;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;

namespace LpbPrj.Client.Tools.Helper
{
    public class RegHelper
    {
        /// <summary>
        /// 获取注册表值
        /// </summary>
        /// <param name="regKey"></param>
        /// <returns></returns>
        public static string RegGetValue(string regKey, string regKeyName = @"SuoerApp")
        {
            var productName = FileVersionInfo.GetVersionInfo(Assembly.GetEntryAssembly().Location).ProductName;

            List<string> regKeyNames = regKeyName.Split('\\').ToList();
            regKeyNames.Add(productName);

            //LocalMachine必须管理员权限才行
            RegistryKey key = Registry.CurrentUser;
            List<string> subkeyNames = key.GetSubKeyNames().ToList();
            for (int i = 0; i < regKeyNames.Count; i++)
            {
                if (subkeyNames.Contains(regKeyNames[i]))
                {
                    key = key.OpenSubKey(regKeyNames[i], false);
                    subkeyNames = key.GetSubKeyNames().ToList();
                }
            }

            var regValue = key.GetValue(regKey)?.ToString();
            return regValue;
        }

        /// <summary>
        /// 更新注册表值
        /// </summary>
        /// <param name="regKey"></param>
        /// <param name="regValue"></param>
        public static void RegSetValue(string regKey, string regValue,string regKeyName = @"SuoerApp")
        {
            var productName = FileVersionInfo.GetVersionInfo(Assembly.GetEntryAssembly().Location).ProductName;

            List<string> regKeyNames = regKeyName.Split('\\').ToList();
            regKeyNames.Add(productName);

            //LocalMachine必须管理员权限才行
            RegistryKey key = Registry.CurrentUser;
            List<string> subkeyNames = key.GetSubKeyNames().ToList();
            for (int i = 0; i < regKeyNames.Count; i++)
            {
                if (subkeyNames.Contains(regKeyNames[i]))
                {
                    key = key.OpenSubKey(regKeyNames[i], true);
                    subkeyNames = key.GetSubKeyNames().ToList();
                }
                else
                {
                    key = key.CreateSubKey(regKeyNames[i], true);
                    subkeyNames = key.GetSubKeyNames().ToList();
                }
            }

            key.SetValue(regKey, regValue);
        }


        /// <summary>
        /// 设置启动项，需要管理员权限
        /// </summary>
        /// <param name="keyName">键值，启动项名称</param>
        /// <param name="filePath">启动的exe的全路径</param>
        public static void SetAutoRun(string keyName, string filePath)
        {
            using (RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"software\microsoft\windows\currentversion\run", true))
            {
                runKey.SetValue(keyName, filePath);
                runKey.Close();
            }
        }

        static void SetAutoRunTest()
        {
            string keyName = "TestAutoRun";
            string fliePath = @"D:\CSharpProject\CSharp_Learn\MyDelegate.exe";
            SetAutoRun(keyName, fliePath);
        }

    }
}
